package teleradiologia.repository;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sun.util.logging.resources.logging;
import teleradiologia.domain.Address;
import teleradiologia.domain.Radiologylab;
import teleradiologia.utils.HibernateUtil;

public class AddressDaoImpl implements AddressDao {

	@Override
	public long create(Address address) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		long generatedId = (Long)session.save(address);

		tx.commit();
		session.close();
		
		return ++generatedId;	
	}

	@Override
	public void delete(Address address) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.delete(address);

		tx.commit();
		session.close();
	}

	@Override
	public Address getById(long id) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Address address = (Address) session.get(Address.class, new Long(id));
		
		tx.commit();
		session.close();

		return address;
	}

	@Override
	public void update(Address address) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.update(address);

		tx.commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Address> getAll() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		List<Address> addresses = session.createQuery("from Address").list();

		tx.commit();
		session.close();
		
		return addresses;
	}
}
